$(function () {
    back();
    add_into_shopcart();
    init_shopdetail();
});

function init_shopdetail() {
    $.ajax({
        url: '/get-shop-detail',
        type: 'get',
        dataType: 'JSON',
        success: function (data) {
            if (data.status) {
                var result = data.result;
                $('#shop-name').text(result['name']);
                $('#shop-img').attr('src', result['image']);
                $('#shop-price').text(result['price']);
            } else {
                alert('数据获取失败');
            }
        }
    });
}

function back() {
    $('#back-button').click(() => {
        $.ajax({
            url: '/shop-destroy-session',
            type: 'get',
            data: 'JSON',
            success: function (data) {
                window.location.href = '/page/user-main.html';
            }
        });
    });
}

function add_into_shopcart() {
    $('#into-shopcart-button').click(() => {
        $.ajax({
            url: '/add-to-cart',
            type: 'get',
            dataType: 'JSON',
            success: function (data) {
                if (data.status) {
                    $('#into-shopcart-button').attr('class', 'btn btn-success');
                    $('#into-shopcart-button').text('添加购物车成功');
                    setTimeout(function () {
                        window.location.href = '/page/user-main.html';
                    }, 1000);
                } else {
                    $('#into-shopcart-button').attr('class', 'btn btn-danger');
                    $('#into-shopcart-button').text('添加购物车失败');
                }
            }
        });
        
    });
}